package org.example;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.UUID;

@Slf4j
@Service
public class OrderService {

    @Autowired
    private MessageProducer messageProducer;
    
    // 创建订单并发送消息
    public void createOrder(String userId, BigDecimal amount) {
        // 模拟创建订单
        Long orderId = System.currentTimeMillis();
        String orderNo = "ORDER_" + UUID.randomUUID().toString().substring(0, 8);
        Order order = new Order(orderId, orderNo, amount, userId);
        
        log.info("创建订单: {}", order);
        
        // 发送订单创建消息
        messageProducer.sendOrderMessage("ORDER_TOPIC", order);
        
        // 发送延迟消息，30秒后检查订单是否支付
        messageProducer.sendDelayMessage("ORDER_CHECK_TOPIC", orderNo, 4); // 4对应30秒延迟
    }
    
    // 支付订单
    public void payOrder(String orderNo) {
        log.info("订单支付成功: {}", orderNo);
        messageProducer.sendMessage("ORDER_PAY_TOPIC", orderNo);
    }
    
    // 取消订单
    public void cancelOrder(String orderNo) {
        log.info("取消订单: {}", orderNo);
        messageProducer.sendMessage("ORDER_CANCEL_TOPIC", orderNo);
    }
}